#include "State.h"

#include <QDebug>
#include <QFile>
#include <QTextStream>
#include <QApplication>

#include "Player.h"

State::State(const QString& name) : m_name(name), m_player(NULL)
{
    m_troops = 0;
    QFile file (QApplication::applicationDirPath () + "/Ress/" + name + ".cfg");
    if(!file.open(QIODevice::ReadOnly | QIODevice::Text | QIODevice::Truncate))
        qDebug() << "Erreur critique : impossible d'ouvrir le fichier \"" << QApplication::applicationDirPath () << "/Ress/" << name << ".cfg\"";

    m_name = file.readLine();
    m_name.resize(m_name.size()-1);
    m_continent = file.readLine();
    m_continent.resize(m_continent.size()-1);
    QString coord = file.readLine();

    while(!file.atEnd())
    {
        QString t = file.readLine();
        if(t.isEmpty() || t.size() == 1)
            continue;
        if(t.right(1) == "\n")
            t.resize(t.size()-1);
        m_connexions.append(t);
    }

    file.close();
}

void State::setPlayer(Player* player)
{
    if(m_player != NULL)
        m_player->removeState(this);

    m_player = player;
    m_player->addState(this);
}
